# 共用的自定义工具类
import functools
from flask import g, session, current_app

from info.models import User


def do_index_class(index):
    if index == 0:
        return "first"
    elif index ==1:
        return "second"
    elif index ==2:
        return "third"
    return ""


def user_login_data(f):
    @functools.wraps(f)  # 使用functools.wraps来装饰内层函数，可以保持当前装饰器去装饰函数的__name__的值不变
    def wrapper(*args,**kwargs):
        # 1.查询用户登录信息
        user_id = session.get("user_id", None)
        user = None
        if user_id:

            # ２．尝试查询用户信息
            try:
                user = User.query.get(user_id)
            except Exception as e:
                current_app.logger.error(e)
        g.user = user
        return f(*args,**kwargs)
    return wrapper
